package de.fuberlin.inf.alp2;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import de.fuberlin.inf.alp2.utils.ConsoleInput;

public class PolygonsClient extends Frame implements WindowListener {
	private static final long serialVersionUID = 1L;
	public static Polygon polygon;
	static final int SIZE = 100;
	static final int F = 6;
	
	public PolygonsClient(){
		this.addWindowListener(this);
	}

	public static void paint(Graphics g, Color color) throws Exception {
//		long l = 0xC0B0L;
//		float f = 1.4f + 1.40f;
		if (!polygon.isPolygon())
			throw new Exception("No polygon given!");
		g.setColor(color);
		Point p1 = null;
		Point p2 = null;
		p1 = polygon.getPoints().get(polygon.getPoints().size() - 1);
		p2 = polygon.getPoints().get(0);
		g.drawLine(p1.getX() * F, (SIZE - (p1.getY())) * F, p2.getX() * F,
				(SIZE - p2.getY()) * F);
		for (int i = 1; i < polygon.getPoints().size(); i++) {
			p1 = p2;
			p2 = polygon.getPoints().get(i);
			g.drawLine(p1.getX() * F, (SIZE - (p1.getY())) * F, p2.getX() * F,
					(SIZE - p2.getY()) * F);
		}
	}
	public void paint(Graphics g) {
		try {
			paint(g, Color.red);
		}
		catch (Exception exc){}
	}
	
	@SuppressWarnings("deprecation")
    public static void showPolygon() throws Exception {
		System.out.println("Perimeter: " + polygon.perimeter());
		System.out.println("centroid: " + polygon.center());
		polygon.shift(30,-10);

		PolygonsClient pp = new PolygonsClient();
		pp.setSize(SIZE*F+100,SIZE*F+100);
		pp.setTitle("PolygonPainter");
		pp.show();
	}
	
	public static void main(String args[]) {
		int userinput = -1;
		while (userinput != 0)
		{
			String message = "Which implementation of polygon do you want to " +
					"check?\n" +
					"    " + PolygonFactory.ListPolygon + ". ListPolygon\n" +
					"    " + PolygonFactory.ArrayPolygon + ". ArrayPolygon\n" +
					"    0. Exit\n";
			userinput = ConsoleInput.readInt(message, "Try again!");
			if (userinput != 0)
			{
				try {
					polygon = PolygonFactory.newPolygon(userinput);
				} catch (Exception e) {
					System.out.println(e.getMessage());;
				}
				message = "Please select.\n" +
						"    1. Use default polygon {(10,10),(50,10),(20,20)," +
						"(70,70),(50,80),(5,90)}\n" +
						"    2. Create a custom polygon.\n" +
						"    0. Exit\n";
				userinput = ConsoleInput.readInt(message, "Try again!");
				try{
					switch (userinput) {
					case 1:
						polygon.addPoint(new Point(10,10));
						polygon.addPoint(new Point(50,10));
						polygon.addPoint(new Point(20,20));
						polygon.addPoint(new Point(70,70));
						polygon.addPoint(new Point(50,80));
						polygon.addPoint(new Point(5,90));
						showPolygon();
						break;
					case 2:
						while (userinput < 3)
							userinput = ConsoleInput.readInt("How many points?",
									"Try again!");
						for (int i = 0; i < userinput; i++)
						{
							System.out.println("Adding " + (i+1) + ". Point");
							int x, y;
							x = ConsoleInput.readInt("Abscissa (x-coordinate)?",
									"Enter an integer!");
							y = ConsoleInput.readInt("Ordinate (y-coordinate)?",
									"Enter an integer!");
							polygon.addPoint(new Point(x,y));
						}
						showPolygon();
						break;
					default:
						break;
					}
					
				}
				catch(Exception exc)
				{
					System.out.println("Error occured\n"+exc.getMessage());
				}
			}
		}
		System.out.println("Close window to exit.");
	}
	@Override
	public void windowActivated(WindowEvent e) {
	}
	@Override
	public void windowClosed(WindowEvent e) {
	}
	@Override
	public void windowClosing(WindowEvent e) {
		this.dispose();
	}
	@Override
	public void windowDeactivated(WindowEvent e) {
	}
	@Override
	public void windowDeiconified(WindowEvent e) {
	}
	@Override
	public void windowIconified(WindowEvent e) {
	}
	@Override
	public void windowOpened(WindowEvent e) {
	}
}
